<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\Article;
use App\Http\Model\Category;
use App\Http\Model\Links;

class IndexController extends CommonController
{
    //首页显示
    public function index(){

        //点击量最高的6篇文章
        $hot = Article::orderBy('art_view','desc')->take(6)->get();
                
        //图文列表5篇（带分页）
        $data = Article::orderBy('art_time','desc')->paginate(5);
        
        //友情链接
        $links = Links::orderBy('link_order','asc')->get();
        
        return view('home.index',compact('hot','data','news','links','hot_5'));
    }
    
    //列表页
    public function cate($cate_id){
        $cates = Category::find($cate_id);
        if(!$cates){
            echo "<div style='margin:0 auto'><h1>没有您找的分类</h1></div>";
            die;
        }
        //图文列表4篇（带分页）
        $data = Article::where('cate_id',$cate_id)->orderBy('art_time','desc')->paginate(4);

        //子分类
        $subcate = Category::where('cate_pid',$cate_id)->orderBy('cate_order','asc')->get();

        return view('home.cate',compact('cates','data','subcate'));
    }
    
    //文章页
    public function article($art_id){
        //获得某篇文章
        $data = Article::join('category','article.cate_id','=','category.cate_id')->where('art_id',$art_id)->first();
        if(!$data){
            echo "<div style='margin:0 auto'><h1>没有您找的文章</h1></div>";
            die;
        }
        //刷新自增点击数
        Article::where('art_id',$art_id)->increment('art_view');
        $data->art_view = Article::where('art_id',$art_id)->value('art_view');
        //当前位置
        if($data->cate_pid!=0){
            $data->_pid_name = Category::where("cate_id",$data->cate_pid)->value('cate_name');
        }

        //上一篇、下一篇
        $article['pre'] = Article::where('art_id','<',$art_id)->orderBy('art_id','desc')->first();
        $article['next'] = Article::where('art_id','>',$art_id)->orderBy('art_id','asc')->first();

        //相关文章
        $relevant = Article::where('cate_id',$data->cate_id)->take(6)->get();
        
        return view('home.article',compact('data','article','relevant'));
    }
}
